import scrapy

from jdphone.items import JdphoneItem

class jdphone(scrapy.Spider) :
    name = 'jdphone1'

    def start_requests(self) :
        url = "https://list.jd.com/list.html?cat=9987,653,655&page=1&delivery=1&stock=0&sort=sort_rank_asc&trans=1&JL=4_10_0#J_main"
        yield scrapy.Request(url=url, callback=self.parse)

    def parse(self, response) :
        phones = response.xpath('//div[@class="gl-i-wrap j-sku-item"]')
        print(len(phones))
        for phone in phones :
            item = JdphoneItem()
            item['sku'] = phone.xpath('@data-sku').extract_first()
            item['name'] = phone.xpath('.//div[@class="p-name"]/a/em/text()').extract_first().strip()
            item['shop'] = phone.xpath('.//div[@class="p-shop"]/@data-shop_name').extract_first()
            yield {
                'sku' : item['sku'],
                'name': item['name'],
                'shop': item['shop'],
            }
        next_page = phones.xpath('//a[@class="pn-next"]/@href').extract_first()
        if next_page is not None :
            next_page = response.urljoin(next_page)
            yield scrapy.Request(url=next_page, callback=self.parse)